package com.example.service.impl;

import com.example.feignClients.CategoryClient;
import com.example.mapper.BookMapper;
import com.example.pojo.dto.*;
import com.example.pojo.entity.Book;
import com.example.pojo.entity.Category;
import com.example.pojo.entity.result.PageResult;
import com.example.pojo.entity.result.Result;
import com.example.pojo.vo.BookListVO;
import com.example.pojo.vo.BookVO;
import com.example.service.BookService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import context.BaseContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.PinYinUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
public class BookServiceImpl implements BookService {
    private final BookMapper bookMapper;
    private final CategoryClient categoryClient;

    /**
     * 图书入库
     *
     * @param bookInsertDTO
     */
    @Override
    @Transactional
    public void insertBookWithCategory(BookInsertDTO bookInsertDTO) {
        //操作分类表
        CategoryInsertDTO categoryInsertDTO = CategoryInsertDTO.builder()
                .bookName(bookInsertDTO.getName())
                .author(bookInsertDTO.getAuthor())
                .publishingHouse(bookInsertDTO.getPublishingHouse())
                .build();
        Result<Long> result = categoryClient.insertCategory(categoryInsertDTO);
        Long categoryId = result.getData();


        //新增图书
        bookInsertDTO.setCreateTime(LocalDateTime.now());
        bookInsertDTO.setCategoryId(categoryId);
        bookInsertDTO.setStatus(1);
        bookInsertDTO.setEnableLend(1);
        //员工登录后自动加入
        bookInsertDTO.setDepositor(BaseContext.getCurrentId());

        String NameHeadChar = PinYinUtils.getPinYinHeadChar(bookInsertDTO.getName());
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        bookInsertDTO.setId(categoryId + NameHeadChar + uuidStr);

        bookMapper.insertBook(bookInsertDTO);
    }

    /**
     * 根据图书ID删除图书
     *
     * @param bookDeleteDTO
     */
    @Override
    public void deleteBook(BookDeleteDTO bookDeleteDTO) {
        Book book = bookMapper.getById(bookDeleteDTO.getId());
        bookMapper.deleteBook(bookDeleteDTO);
        categoryClient.decrease(book.getCategoryId());
    }

    /**
     * 根据图书分类id分页查询图书列表
     *
     * @param bookListDTO
     * @return
     */
    @Override
    public PageResult<BookListVO> listByCategoryId(BookListDTO bookListDTO) {
        //设置分页条件
        PageHelper.startPage(bookListDTO.getPage(), bookListDTO.getPageSize());

        //执行查询
        List<Book> bookList = bookMapper.listByCategoryId(bookListDTO);

        Page<Book> page = (Page<Book>) bookList;

        List<BookListVO> bookListVOList = new ArrayList<>();
        for (Book book : bookList) {
            BookListVO bookListVO = new BookListVO();
            BeanUtils.copyProperties(book, bookListVO);
            bookListVOList.add(bookListVO);
        }

        //返回分页结果
        return new PageResult<>(page.getTotal(), bookListVOList);
    }

    /**
     * 根据图书编号查询图书信息
     *
     * @param id
     * @return
     */
    @Override
    public BookVO getById(String id) {
        Book book = bookMapper.getById(id);
        BookVO bookVO = new BookVO();
        if (book != null) {
            BeanUtils.copyProperties(book, bookVO);
        }
        return bookVO;
    }

    /**
     * 修改图书信息
     *
     * @param bookUpdateDTO
     */
    @Override
    public void updateById(BookUpdateDTO bookUpdateDTO) {
        bookMapper.updateById(bookUpdateDTO);
    }
}
